package driver.dao;

import driver.entity.Payfor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class PayforDaoImpl_a  implements PayforDao_a  {
    //private BaseDao dao=new BaseDao();
    @Override
    public ArrayList<Payfor> select_all() throws SQLException {
        Connection conn=new JDBCUtils().getConn();
        //select payfor.*,user.name,user.phonenum  from payfor,school,user where payfor.school_id=school.id and school.phonenum=18292761793 and user.id=payfor.user_id;
        //SELECT payfor.*, user.name,user .phonenum FROM payfor,user WHERE payfor.user_id = USER .id;
        PreparedStatement prst=conn.prepareStatement("select payfor.*,user.name,user.phonenum  from payfor,school,user where payfor.school_id=school.id and user.id=payfor.user_id;");
        /*String[] data=new String[]{};*/
        /*for (int i = 0; i < data.length; i++) {
            prst.setObject(i+1,data[i]);
        }*/
        //Connection conn = dao.getConn();
        //ResultSet result = dao.executeQuery("SELECT * FROM payfor");
        ResultSet result=prst.executeQuery();
        ArrayList<Payfor> list=new ArrayList<>();
        while(result.next())
        {
            Payfor payfor=new Payfor();
            payfor.setId(result.getInt(1));
            payfor.setTime(result.getTimestamp(4));
            payfor.setExamtime(result.getDate(5));
            payfor.setExamadd(result.getString(6));
            payfor.setPass122(result.getString(7));
            payfor.setPayitem(result.getString(8));
            payfor.setIdcard1_src(result.getString(9));
            payfor.setIdcard2_src(result.getString(10));
            payfor.setUnpass_src(result.getString(11));
            payfor.setFlag(result.getInt(12));
            payfor.setValue(result.getString(13));
            payfor.setName(result.getString(14));
            payfor.setPhonenum(result.getString(15));
            list.add(payfor);
        }
        JDBCUtils.close(result,prst,conn);
        //dao.closeAll();



        return list;
    }

    @Override
    public boolean handle(Payfor payfor) throws SQLException {
        //AND payfor.user_id = user .id
        Connection conn=new JDBCUtils().getConn();
        PreparedStatement prst=conn.prepareStatement("UPDATE payfor SET flag=?,value=? WHERE id=?");
       Object[] data=new Object[]{payfor.getFlag(),
                payfor.getValue(),
                payfor.getId()};
        for (int i = 0; i < data.length; i++) {
            prst.setObject(i+1,data[i]);
        }
        boolean flag=/*dao.executeUpdate("UPDATE payfor SET flag=?,value=? WHERE id=? ",
                payfor.getFlag(),
                payfor.getValue(),
                payfor.getId());*/prst.executeUpdate()>0;
       // dao.closeAll();
        JDBCUtils.close(prst,conn);
        return flag;

    }
}
